#include "log_init.h"
#include <QApplication>
#include <QDir>
#include <iostream>
LogInit * LogInit::logInit = new LogInit;
void logFunction(const QString &message, QsLogging::Level level)
{
    std::cout << "From log function: " << qPrintable(message) << " " << static_cast<int>(level)
              << std::endl;
}

void LogInit::init(QsLogging::Level logLevel, QString filename, int maxByte, uint8_t maxOldFile)
{
    using namespace QsLogging;
    // 1. init the logging mechanism
    Logger& logger = Logger::instance();
    logger.setLoggingLevel(logLevel);
    const QString sLogPath(QDir(QApplication::applicationDirPath()).filePath(filename));

    // 2. add two destinations
    DestinationPtr fileDestination(DestinationFactory::MakeFileDestination(
                                       sLogPath, EnableLogRotation, MaxSizeBytes(maxByte), MaxOldLogCount(maxOldFile)));
    DestinationPtr debugDestination(DestinationFactory::MakeDebugOutputDestination());
    //DestinationPtr functorDestination(DestinationFactory::MakeFunctorDestination(&logFunction));
    logger.addDestination(debugDestination);
    logger.addDestination(fileDestination);
    //logger.addDestination(functorDestination);
    // 3. start logging
//    QLOG_INFO() << "Program started";
//    QLOG_INFO() << "Built with Qt" << QT_VERSION_STR << "running on" << qVersion();

//    QLOG_TRACE() << "Here's a" << QString::fromUtf8("trace") << "message";
//    QLOG_DEBUG() << "Here's a" << static_cast<int>(QsLogging::DebugLevel) << "message";
//    QLOG_WARN()  << "Uh-oh!";
//    qDebug() << "This message won't be picked up by the logger";
//    QLOG_ERROR() << "An error has occurred";
//    qWarning() << "Neither will this one";
//    QLOG_FATAL() << "Fatal error!";

}

void LogInit::setLogLevel(uint8_t level)
{
    QsLogging::Logger::instance().setLoggingLevel(QsLogging::Level(level));
}

LogInit::LogInit(QObject *parent) : QObject(parent)
{


}
